﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Themn_DL_MasterPage3 : System.Web.UI.MasterPage
{
    PowerCreator.LMS.Web.DL.User.WebLearner opUser = new PowerCreator.LMS.Web.DL.User.WebLearner();
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.ViewQuery.ViewQuery objViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Grade.GradeManager objGradeManager = new PowerCreator.LMS.Web.Grade.GradeManager();
    PowerCreator.LMS.Web.Course.VirtualDirectory opVirtual = new PowerCreator.LMS.Web.Course.VirtualDirectory();
    PowerCreator.LMS.Web.DL.Project.TOPinion opPinion = new PowerCreator.LMS.Web.DL.Project.TOPinion();
    PowerCreator.LMS.Course.DB.TCourseResourceManager opCR = new PowerCreator.LMS.Course.DB.TCourseResourceManager();
    PowerCreator.LMS.ViewQuery.ViewQuery vq=new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.BBS.ManagerBBS opBBS = new PowerCreator.LMS.Web.BBS.ManagerBBS();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinderUserLogin();

            BinderNews_TZGG();
            BinderBBS();
            rptRefer();
            rptClickResource();
            rptClickCourse();
        }
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            tb_login.Visible = true;
            tb_loginout.Visible = false;
        }
        else
        {
             PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
             if (login.VOLearner == null)
             {
                 Session.Abandon();
                 PowerCreator.LMS.Common.Script.Script.ReloadMe();

             }
             else
             {
                 tb_login.Visible = false;
                 tb_loginout.Visible = true;
                 lbl_username.Text = login.UserName.Trim();
                 lbl_UserReal.Text = login.RealName.Trim();
             }
        }
    }

    protected void lnkbtn_EN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "1";
    }
    protected void lnkbtn_CN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "0";
    }
    protected void BinderUserLogin()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {

            //lal_UserLogin.Text = "<a  href=\"UserLogin.aspx\">登陆</a> |  <a  href=\"Register.aspx\">注册</a>";
            //btn_out.Visible = false;
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                //lal_UserLogin.Text = "<a  href=\"UserLogin.aspx\">登陆</a> |  <a  href=\"Register.aspx\">注册</a>";
                //btn_out.Visible = false;

            }
            else
            {
                //lal_UserLogin.Text = login.RealName.Trim() + ",欢迎光临大连市教师在线学习中心！| ";
                //btn_out.Visible = true;
            }
        }
    }
    protected void btn_out_Click(object sender, EventArgs e)
    {
        //PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
        //opUser.UpdateOnlineUserByUserID(GetLoginEntity.UserID);
        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }

    protected void btn_Login_Click(object sender, EventArgs e)
    {
        //登录
        //登录信息
        PowerCreator.LMS.Web.DL.User.LoginInfo info = new PowerCreator.LMS.Web.DL.User.LoginInfo();

        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();

        if (!CheckPage()) return;

        info.LoginIp = PowerCreator.LMS.Common.Utils.IPUtils.GetIP;
        info.LoginTime = DateTime.Now;
        info.UserName = txt_UserName.Text;
        info.UserPwd = txt_UserPwd.Text;
        string UserLocation = GetUserLocation();

        if (opUser.Login(info, ref login, ConfigurationManager.AppSettings["CheckLoginIP"].ToString(), UserLocation))
        {
            Session.Add(PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO, login);
            //opUser.UpdateOnlineUserList();
            //login.VOOnlineUser = opUser.AddOnlineUser(info, login.VOUser.UserId);
            PowerCreator.LMS.Common.Script.Script.ReloadMe();

            //btn_Login.Visible = false;
            //btn_LoginOut.Visible = true;

        }
        else
        {
            opUser.AddOnlineUser(info, PowerCreator.LMS.Web.User.User.LoginNoUserID);
            string msg = config.GetLanagerValueByKey(login.LoginMessage, PowerCreator.LMS.Language.LanagetItemType.MSG);

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "al", msg);
        }
    }

    private bool CheckPage()
    {
        if (string.IsNullOrEmpty(txt_code.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的五位验证码！");
            return false;
        }
        if (Session["CheckCode"] != null)
        {
            string s = Session["CheckCode"].ToString();
            if (s.ToLower() != txt_code.Text.ToLower())
            {
                //lblLoginMsg.Text = config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page);
                PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的五位验证码！");
                txt_code.Text = "";
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (string.IsNullOrEmpty(txt_UserName.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的帐户！");
            return false;
        }
        if (string.IsNullOrEmpty(txt_UserPwd.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的密码！");
            return false;
        }
        return true;

    }

    public string GetUserLocation()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            return string.Empty;
        }
    }

    protected void btn_LoginOut_Click(object sender, EventArgs e)
    {
        //PowerCreator.LMS.Web.Base.BasePage obj=new PowerCreator.LMS.Web.Base.BasePage();
        //if (obj.GetLoginEntity != null)
        //{
        //    opUser.UpdateOnlineUserByUserID(obj.GetLoginEntity.UserID);
        //}
        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }



    public void rptClickResource()//课件点击排行
    {
        string search = "";
        string orderBy = "ResourceClickNum desc";
        DataTable dt = opResource.QueryResource(search, orderBy, 6, 1);
        rptClkResource.DataSource = dt;
        rptClkResource.DataBind();

    }


    public void rptClickCourse()//课程观看排行
    {
        string search = "";
        string orderBy = "CourseName desc";
        //string orderBy = "CourseCreateTime desc";
        DataTable dt = opCourse.QueryCourse(search, orderBy, 8, 1);

        DataTable dt1 = new DataTable();
        dt.Columns.Add("CilckNum");
        string sql="";
        for (int i=0;i<dt.Rows.Count;i++)
        {
            if (dt.Rows[i]["CourseName"].ToString().Length > 13)
            {
                dt.Rows[i]["CourseName"] = dt.Rows[i]["CourseName"].ToString().Substring(0, 13);
            }
            sql="select sum(ResourceClickNum) as sums from TResource where ResourceID in (select FKCoursersResourceID from TCourseResource where FKCourseresCourseID="+dt.Rows[i]["Course_ID"]+")";
            dt1=vq.Query(sql);
            if(dt1.Rows.Count>0)
               dt.Rows[i]["CilckNum"]=dt1.Rows[0]["sums"].ToString();
            else
              dt.Rows[i]["CilckNum"]="0";
            if (dt.Rows[i]["CilckNum"].ToString().Length == 0)
                dt.Rows[i]["CilckNum"] = "0";
        }
        rptClkCourse.DataSource = dt;
        rptClkCourse.DataBind();
       

    }


    private void BinderNews_TZGG()
    {
        string search = " NewState = 1 and ((FKNewTrainclaIDList is null) or (FKNewTrainclaIDList='')) and NewID not in (select top 5 NewID from Tnew where NewPictureUrl is not null order by NewPublishTime desc) and FKNewNewTypeID = (select top 1 NewTypeID from TNewType where NewTypeName='通知公告' )  ";
        string orderBy = " NewPublishTime desc ";
        DataTable dt = opNews.Query(search, orderBy, 7, 1);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["NewUrl"] == null)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();
            else if (dt.Rows[i]["NewUrl"].ToString().Length == 0)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();
        }
        rpt_NewList_TZGG.DataSource = dt;
        rpt_NewList_TZGG.DataBind();

    }

    private void BinderBBS()
    {
        string search = "";
        string orderby = " ThemeLastRevertTime desc,ThemeCreatetime desc";
        DataTable dt = new DataTable();
        dt = opBBS.QueryTheme(search, orderby, 7, 1);
        rpt_bbs.DataSource = dt;
        rpt_bbs.DataBind();

    }

    public void rptRefer()//评论信息
    {
        string search = "Type=0";
        string orderBy = "ID desc";
        DataTable dt = opPinion.Query(search, orderBy, 6, 1);
        if (dt.Rows.Count < 6)
        {
            
            int maxid = int.Parse(dt.Rows[0]["ID"].ToString());
            int num = dt.Rows.Count;
            for (int i = num; i < 6; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = maxid+i;
                dr["Title"] = "&nbsp;";
                dt.Rows.Add(dr);
                //dt.Rows.Remove(dr);
                //dt.Rows.InsertAt(dr, i);
            }
        }
        rpt_Refer.DataSource = dt;
        rpt_Refer.DataBind();

    }

}
